﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqCountry
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);
        public List<Country> GetCountries(string sortExpression, int page)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<CountryEntity> query = db.CountryEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "CodeId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.Code_Id);
                            else
                                query = query.OrderByDescending(c => c.Code_Id);
                            break;

                        //case "BranchName":
                        //    if (sortOrder == "ASC")
                        //        query = query.OrderBy(c => c.BranchName);
                        //    else
                        //        query = query.OrderByDescending(c => c.BranchName);
                        //    break;
                    }
                }

                
                var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);
                
                return pageData.Select(c => Mapper.ToCodeObject(c)).ToList();
            }
        }


        public List<Country> GetCountries(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<CountryEntity> query = db.CountryEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "CodeId":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.Description);
                            else
                                query = query.OrderByDescending(c => c.Code_Id);
                            break;

                        //case "BranchName":
                        //    if (sortOrder == "ASC")
                        //        query = query.OrderBy(c => c.BranchName);
                        //    else
                        //        query = query.OrderByDescending(c => c.BranchName);
                        //    break;
                    }
                }


                //var pageData = query.Skip((page - 1) * pageSize).Take(pageSize);

                return query.Select(c => Mapper.ToCodeObject(c)).ToList();
            }
        }


        public Country GetCountry(int codeId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToCodeObject(db.CountryEntities
                            .SingleOrDefault(p => p.Code_Id == codeId));
            }
        }


        public void InsertCodeCountry(Country code)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {

                    CountryEntity entity = Mapper.ToCodeEntity(code);
                    db.CountryEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    code.CodeId = entity.Code_Id;
                    
                    
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to create code country record was made before your changes.");
                }
                   
                catch (Exception)
                {
                    
                    throw new Exception("Unable to Insert code country " + code.Code);
                }
            }
        }

        public int UpdateCode(Country code)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    CountryEntity entity = Mapper.ToCodeEntity(code);
                    db.CountryEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to update code record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public int DeleteCodeCountry(Country code)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    CountryEntity entity = Mapper.ToCodeEntity(code);
                    db.CountryEntities.Attach(entity, false);
                    db.CountryEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to delete code was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


        //public List<Branch> GetBranchesFromGroup(int expression)
        //{
        //    using (SODataDataContext db = DataContextFactory.CreateContext())
        //    {
        //        IQueryable<BranchEntity> qry = db.BranchEntities;

        //        /* qry=from c in db.SOBranches
        //             where c.GroupId==expression
        //             select new{
        //                 c.BranchName,c.EffectiveFrom,c.EffectiveTo

        //                 };
        //         * */

        //        qry = qry.OrderBy(c => c.BranchName);

        //        return qry.Select(c => Mapper.ToBranchObject(c)).ToList();

        //    }

        //}

    }
}
